// pages/activity/signup/signup.js
//导入验证js
import WxValidate from "../../unity/WxValidate.js";
var util = require("../../unity/common.js");

const app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticUrl: app.globalData.staticUrl,
    ischeck: false,
    activityid:"",
    registerinfo: {
      registerID: "",
      registertime: "",
      childname: "",
      phoneno: "",
      high: "",
      weight: "",
      address: "",
      ac_id:"",
      age:"",
      sex:1,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var activityid = options.activityid;
    this.setData({activityid:activityid});
    if(!activityid){
      wx.navigateBack();
      return;
    }
    this.getregisterinfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initValidate();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 

  
  changedata:function(e){
   var registerinfo = this.data.registerinfo;
    var cat = e.target.id;
    var value = e.detail.value;
    console.log("changedata");
    console.log(e);
    if (cat == "sex") {
      registerinfo.sex = value;
    } 
    console.log(registerinfo);
    this.setData({
      registerinfo: registerinfo
    });
  },


  inputdata: function (e) {
    var registerinfo = this.data.registerinfo;
    var cat = e.target.id;
    var value = e.detail.value;
    console.log("inputdata");
    console.log(e);
    if (cat == "childname") {
      registerinfo.childname = value;
    } else if (cat == "phoneno") {
      registerinfo.phoneno = value;
    } else if (cat == "high") {
      registerinfo.high = value;
    } else if (cat == "address") {
      registerinfo.address = value;
    } else if (cat == "weight") {
      registerinfo.weight = value;
    }
    else if (cat == "age") {
      registerinfo.age = value;
    }

    console.log(registerinfo);
    this.setData({
      registerinfo: registerinfo
    });
  },

  filldata: function (registerinfo) {
    console.log(registerinfo);
    var registerinfo = { //增加playerinfo子元素
      childname: registerinfo.UserTrueName,
      phoneno: registerinfo.UserBirthday,
      high: registerinfo.UserCardNumber,
      address: registerinfo.comptioncategory,
      weight: registerinfo.comptiongroup,
      age:registerinfo.age,
      sex:registerinfo.sex,

    }


    this.setData({

      registerinfo: registerinfo,
    })

    console.log("this.data.registerinfo");
    console.log(this.data.registerinfo);
    this.initValidate();
  },


  getregisterinfo() {
    var that = this;
    wx.showLoading({
      title: '正在加载报名信息',
    })
    app.getactivityregisterinfo(this.data.activityid,function (res) {
        console.log("registerinfo");

       

          if (res.data.data && res.data.data.registerinfo && res.data.data.registerinfo.registerID) {

            that.setData({
              registerinfo: res.data.data.registerinfo
            });

            console.log(res.data.data);
          }

        
      },
      function () {
        wx.hideLoading();
      }
    );
  },



  ///报名提交方法
  _registerformSubmit(e) {
    var registerinfo = this.data.registerinfo;
    console.log(registerinfo);
    if (!this.WxValidate.checkForm(registerinfo)) {
      //表单元素验证不通过，此处给出相应提示

      let error = this.WxValidate.errorList[0];
      console.log(error.param);




      switch (error.param) {
        case "childname":

          //TODO
          break;
        case "phoneno":
          //TODO
          break;
        case "high":
          //TODO
          break;
        case "address":
          //TODO
          break;
        case "weight":
          //TODO
          break;




      }
      console.log(util);
      // this.data.focuslist.removeChild();
      this._errorshow(error.msg);
      util.commontscrolltoview(error.param, this);

      return false;
    }



    console.log(registerinfo);

    registerinfo.ac_id = this.data.activityid;
    registerinfo.user_id = app.globalData.userInfo.userID;
    registerinfo.userOpenid = app.globalData.userInfo.openid;

    this.setData({
      ischeck: true,
      registerinfo: registerinfo
    });
    util.scrolltoview("dbcheck", this);
    return true;
  },

  commit: function () {
    if(!this.data.ischeck){
      wx.showToast({
          icon:"none",
          title:"请勾选已同意",
          duration:2000,
    });
      return ;
    }
    if(!this._registerformSubmit()){
      return ;
    }
    var that = this;
    that.setData({
      iscommit: true
    });
    wx.showLoading({
      title: '正在提交信息',
    })

    app.commitactivityregister(this.data.registerinfo, function (res) {
      console.log("commit");
      console.log(res);
      if (res.data.errorcode.code = "ok") {
        var succuss = {
          message: "活动报名信息提交成功",
          time: new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString(),
          back: true,
          buttoninfo: "返回"
        };
        var message = "活动报名信息提交成功";

        
        succuss.message = message;

        wx.redirectTo({
          url: '/pages/succuss/index?succuss=' + JSON.stringify(succuss)

        })
      } else {
        wx.showToast({
          title: res.data.errorcode.message,
          duration: 5000,
        })
      }

    }, function () {
      that.setData({
        iscommit: false
      });
      wx.hideLoading();
    })
  },

  back: function () {
    this.setData({
      ischeck: false
    });
  },


  _errorshow(msg) {
    this.setData({
      error: msg
    })
    var error = this.selectComponent('#toptips');
    error._error(this.data.error);
  },



  initValidate() {



    let rules = {
      childname: {
        required: true,
        maxlength: 10
      },
      
      address: {
        required: true,
        maxlength: 100
      },

      phoneno: {
        required: true,
        tel: true,

      },


    }

    let message = {
      childname: {
        required: '请输入姓名',
        maxlength: '名字不能超过10个字'
      },
      sex:{
        required:'请选择孩子性别',

      },
      high: {
        required: '请输入孩子身高',
        number: '请输入正确的身高数据',
      },
      weight: {
        required: '请孩子体重',
        number: '请输入正确的体重数据',
      },
      address: {
        required: '请输入地址',
        maxlength: '地址不能超过100个字'
      },
      phoneno: {
        required: '请输入联系方式',
        tel: '请输入正确的联系方式',

      },




    }

    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },

  checkchange:function(){
    this.setData({
      ischeck:!this.data.ischeck
    })
  }
})